package com.ym.livedemo.api

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ym.livedemo.rxLaunch
import com.ym.livedemo.toRequestBody
import splitties.toast.toast

class BusinessViewModel: ViewModel() {
    fun login(
        map: Map<String, Any>, func: (
            BaseResultEntity<LoginResult>
        ) -> Unit
    ) {
        viewModelScope.rxLaunch<BaseResultEntity<LoginResult>> {
            onSuccess = {
                func.invoke(it)
            }
            onError = {
                toast("登录失败")
            }
            onRequest = {
                ApiServiceClient.loginApiService.login(
                    toRequestBody(
                        map
                    )
                )
            }
        }
    }

    fun getLiveUrl(func: (String) -> Unit) {
        viewModelScope.rxLaunch<BaseResultEntity<String>> {
            onSuccess = {
                func.invoke(it.data)
            }
            onError = {
                toast("获取推流链接失败")
            }
            onRequest = {
                ApiServiceClient.apiService.getLiveUrl()

            }
        }
    }

    fun getLiveCode(func: (String) -> Unit) {
        viewModelScope.rxLaunch<BaseResultEntity<String>> {
            onSuccess = {
                func.invoke(it.data)
            }
            onError = {
                toast("获取推流码失败")
            }
            onRequest = {
                ApiServiceClient.apiService.getLiveCode()

            }
        }
    }

    fun getLivePullUrl(resolve: Int, func: (String) -> Unit) {
        viewModelScope.rxLaunch<BaseResultEntity<String>> {
            onSuccess = {
                func.invoke(it.data)
            }
            onError = {
                toast("获取拉流地址失败")
            }
            onRequest = {
                ApiServiceClient.apiService.getLivePullUrl(resolve)

            }
        }
    }


}